package day02;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/2 20:14
 * 力扣 82
 */
public class LinkedListPractice05 {

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        if (head.val == head.next.val) {
            ListNode x = head.next.next;
            while(x != null && x.val == head.val) {
                x = x.next;
            }
            return deleteDuplicates(x);
        } else {
            head.next = deleteDuplicates(head.next);
            return head;
        }
    }

    public ListNode deleteDuplicates02(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode s = new ListNode(-1, head);
        ListNode p1 = s;
        ListNode p2, p3;
        while((p2 = p1.next) != null && (p3 = p2.next) != null) {
            if (p2.val == p3.val) {
                while((p3 = p3.next) != null && p3.val == p2.val) { }
                // p3找到了非重复值
                p1.next = p3;
            } else {
                p1 = p1.next;
            }
        }
        return s.next;
    }
}
